Allow documenting binary targets.
authorAlex Crichton <alex@alexcrichton.com>
Mon, 4 Aug 2014 14:21:31 +0000 (07:21 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 4 Aug 2014 14:28:57 +0000 (07:28 -0700)
This removes the check in the compilation phase, but adds an error if you're
documenting a library and a binary with the same name (as the rustdoc output
would conflict).

src/cargo/ops/cargo_doc.rs
src/cargo/ops/cargo_rustc/mod.rs
tests/test_cargo_doc.rs

index 6252b2831d0ff74866da9e3357f76be5ed55f30c..1b3264e66dbd9c3ae31443d4996907c054e9fbfa 100644 (file)
@@ -1,5 +1,9 @@
+use std::collections::HashSet;
+
+use core::source::Source;
 use ops;
-use util::CargoResult;
+use sources::PathSource;
+use util::{CargoResult, human};
 
 pub struct DocOptions<'a> {
     pub all: bool,
@@ -8,6 +12,27 @@ pub struct DocOptions<'a> {
 
 pub fn doc(manifest_path: &Path,
            options: &mut DocOptions) -> CargoResult<()> {
+    let mut source = PathSource::for_path(&manifest_path.dir_path());
+    try!(source.update());
+    let package = try!(source.get_root_package());
+
+    let mut lib_names = HashSet::new();
+    let mut bin_names = HashSet::new();
+    for target in package.get_targets().iter().filter(|t| t.get_profile().is_doc()) {
+        if target.is_lib() {
+            assert!(lib_names.insert(target.get_name()));
+        } else {
+            assert!(bin_names.insert(target.get_name()));
+        }
+    }
+    for bin in bin_names.iter() {
+        if lib_names.contains(bin) {
+            return Err(human("Cannot document a package where a library and a \
+                              binary have the same name. Consider renaming one \
+                              or marking the target as `doc = false`"))
+        }
+    }
+
     try!(ops::compile(manifest_path, &mut options.compile_opts));
     Ok(())
 }
index 8e8e80482ad6b3fee9d14962b253c075794ad9c1..91c23b77bcb6179c5069f43f1d8e466b866a6761 100644 (file)
@@ -237,12 +237,6 @@ fn prepare_rustc(package: &Package, target: &Target, crate_types: Vec<&str>,
 
 
 fn rustdoc(package: &Package, target: &Target, cx: &mut Context) -> Work {
-    // Can't document binaries, but they have a doc target listed so we can
-    // build documentation of dependencies even when `cargo doc` is run.
-    if target.is_bin() {
-        return proc() Ok(())
-    }
-
     let kind = KindTarget;
     let pkg_root = package.get_root();
     let cx_root = cx.layout(kind).proxy().dest().dir_path().join("doc");
index cb89187f87bd260b3ab3a9cac16d773986618f73..5442887e9834f4f48c2530f59417e25afe67858f 100644 (file)
@@ -27,36 +27,17 @@ test!(simple {
     assert_that(&p.root().join("target/doc/foo/index.html"), existing_file());
 })
 
-test!(no_build_main {
+test!(doc_no_libs {
     let p = project("foo")
         .file("Cargo.toml", r#"
             [package]
             name = "foo"
             version = "0.0.1"
             authors = []
-        "#)
-        .file("src/lib.rs", r#"
-            pub fn foo() {}
-        "#)
-        .file("src/main.rs", r#"
-            bad code
-        "#);
 
-    assert_that(p.cargo_process("cargo-doc"),
-                execs().with_status(0).with_stdout(format!("\
-{compiling} foo v0.0.1 (file:{dir})
-",
-        compiling = COMPILING,
-        dir = p.root().display()).as_slice()));
-})
-
-test!(doc_no_libs {
-    let p = project("foo")
-        .file("Cargo.toml", r#"
-            [package]
+            [[bin]]
             name = "foo"
-            version = "0.0.1"
-            authors = []
+            doc = false
         "#)
         .file("src/main.rs", r#"
             bad code
@@ -212,4 +193,24 @@ test!(doc_only_bin {
 
     assert_that(&p.root().join("target/doc"), existing_dir());
     assert_that(&p.root().join("target/doc/bar/index.html"), existing_file());
+    assert_that(&p.root().join("target/doc/foo/index.html"), existing_file());
+})
+
+test!(doc_lib_bin_same_name {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/main.rs", "fn main() {}")
+        .file("src/lib.rs", "fn foo() {}");
+
+    assert_that(p.cargo_process("cargo-doc"),
+                execs().with_status(101)
+                       .with_stderr("\
+Cannot document a package where a library and a binary have the same name. \
+Consider renaming one or marking the target as `doc = false`
+"));
 })